#include "adc.h"
#include <C8051F330.H>

volatile short xdata adc_result		= 0;
volatile short xdata adc_low_val	= 0;
volatile short xdata adc_high_val	= 0x03FF;
volatile short xdata adc_debounce	= 0x0200;

//flag for sample complete
volatile unsigned char xdata adc_sampled = 0;



void ADC0_ISR (void) interrupt 10
{
	static unsigned char int_dec=INT_DEC;   // integrate/decimate counter
									   		// we post a new result when
	                                   		// int_dec = 0
	static long accumulator=0L;         	// here's where we integrate the
					                   		// ADC samples
	AD0INT = 0;                        		// clear ADC conversion complete
	               					   		// indicator
	accumulator += ADC0L;               	// read ADC value and add to running
	accumulator += (ADC0H << 8);        	// total
	int_dec--;                          	// update decimation counter
	
	if (int_dec == 0) {                 	// if zero, then post result
		int_dec = INT_DEC;               	// reset counter
		adc_result = accumulator >> 8;
		//result -= 295;
		accumulator = 0L;                	// reset accumulator
		AD0BUSY = 0;		               	// stop sampling the ADC
		adc_sampled = 1;
			
	}
	
	else 
	   AD0BUSY = 1; 		       // sample again
}